package com.atguigu.gmall.product.controller;

import java.util.concurrent.*;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/25 10:19
 * @Version 1.0
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //  创建一个没有返回值
        //  CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //      System.out.println("无返回值");
        //  });
        //  看需求：
        //        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //            int i = 1/0;
        //            return 200;
        //        }).whenComplete((t,u)->{
        //            System.out.println("t:\t"+t);   //  获取上一个结果
        //            System.out.println("u:\t"+u);   //  异常
        //        }).exceptionally((f)->{
        //            System.out.println(f);
        //            return 404;
        //        });
        //        //
        //        System.out.println(integerCompletableFuture.get());//
        //        if (integerCompletableFuture.get().intValue() == 404){
        //            return;
        //        }

        //        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //            // int i = 1/0;
        //            return 200;
        //        }).thenApply(t->{
        //            System.out.println(t);
        //            return 2*t;
        //        }).exceptionally(f->{
        //            return 404;
        //        });
        //        System.out.println(integerCompletableFuture.get());

        //        CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
        //            return 300;
        //        }).thenAccept(c -> {
        //            System.out.println(c);
        //        });
        //
        //        System.out.println(completableFuture.get());

        //        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //                    // int i = 1/0;
        //                    return 200;
        //                }).whenComplete((t, u) -> {
        //                    System.out.println("t:\t"+t);   //  获取上一个结果
        //                    System.out.println("u:\t"+u);   //  异常
        //                })
        //                .thenApply(t -> {
        //                    System.out.println(t);
        //                    return 2 * t;
        //                }).exceptionally(f -> {
        //                    return 404;
        //                });
        //        System.out.println(integerCompletableFuture.get());

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                6,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        //        CompletableFuture<String> completableFutureA = CompletableFuture.supplyAsync(() -> {
        //            return "hello";
        //        },threadPoolExecutor);
        //
        //        CompletableFuture<Void> completableFutureB = completableFutureA.thenAcceptAsync(s -> {
        //            try {
        //                Thread.sleep(3000);
        //            } catch (InterruptedException e) {
        //                throw new RuntimeException(e);
        //            }
        //            System.out.println(s + ":\tB");
        //        },threadPoolExecutor);
        //
        //        CompletableFuture<Void> completableFutureC = completableFutureA.thenAcceptAsync(s -> {
        //            try {
        //                Thread.sleep(1000);
        //            } catch (InterruptedException e) {
        //                throw new RuntimeException(e);
        //            }
        //            System.out.println(s + ":\tC");
        //        },threadPoolExecutor);
        //
        //        System.out.println(completableFutureB.get());
        //        System.out.println(completableFutureC.get());
        CompletableFuture<Long> completableFutureA = CompletableFuture.supplyAsync(() -> {
            //  睡眠
            try {
                Thread.sleep(100);
                System.out.println("completableFutureA---");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 100l;
        },threadPoolExecutor);

        CompletableFuture<Long> completableFutureB = CompletableFuture.supplyAsync(() -> {
            //  睡眠
            try {
                Thread.sleep(50);
                System.out.println("completableFutureB---");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 20l;
        },threadPoolExecutor);

        CompletableFuture<Long> completableFutureC = CompletableFuture.supplyAsync(() -> {
            //  睡眠
            try {
                Thread.sleep(5000);
                System.out.println("completableFutureC---");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 5000l;
        },threadPoolExecutor);

        //  CompletableFuture.allOf(completableFutureA,completableFutureB,completableFutureC).join();
        CompletableFuture.anyOf(completableFutureA,completableFutureB,completableFutureC).join();

        System.out.println("执行后续业务逻辑....");

    }
}
